############################
## BLIS/Swayable: Table 3 ##
############################

# File Description: 
  # Uses data/packages from O_Data.R
  # Replicates Table 3 from Fabric of Repair report (2025)
# Date Last Updated: 2 May 2025

############################

# Creating a summary dataframe of baseline Reparations support by political party
baseline_party_reparations <- 
  df %>% 
  group_by(race, summarized_party) %>% 
  count(rep_baseline_categorical) %>% 
  mutate(Percentage = n/sum(n) * 100) %>% 
  rename(baseline_support = rep_baseline_categorical) %>% 
  add_column(Movement = "Reparations")

# Creating a summary dataframe of baseline Land Back support by political party
baseline_party_landback <- 
  df %>% 
  group_by(race, summarized_party) %>% 
  count(landback_baseline_categorical) %>% 
  mutate(Percentage = n/sum(n) * 100) %>% 
  rename(baseline_support = landback_baseline_categorical) %>% 
  add_column(Movement = "Land Back")

# Combining both Reparations and Land Back dataframes
baseline_party <- bind_rows(baseline_party_reparations, baseline_party_landback)

# Cleaning the combined dataframe for a table
baseline_party_table <-
  baseline_party %>%
  filter(baseline_support == "Support") %>%
  select(-baseline_support)

# Creating table
formatted_table <- baseline_party_table %>%
  # Group by movement to create separate sections
  arrange(Movement, race, desc(summarized_party)) %>%
  select(-Movement) %>%
  # Create the formatted table
  kable(format = "html",
        col.names = c("Identity Group", "Political Affiliation", "Sample Size", "Support (%)"),
        caption = "Support for Reparations and Land Back Movements by Race and Political Affiliation",
        digits = 1) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
                full_width = FALSE) %>%
  # Add styling
  row_spec(0, bold = TRUE) %>%
  # Highlight Reparations and Land Back sections
  pack_rows("Support for Land Back", 1, 6, color = "#333333") %>%
  pack_rows("Support for Reparations", 7, 12, color = "#333333") %>%
  gsub("<table", "<table style='font-family: \"Jost-Regular\", sans-serif;'", .) %>%
  gsub("<caption>", "<caption style='font-weight: bold; font-size: 18px; margin-bottom: 10px;'>", .)

formatted_table
